package com.example.controller;


import com.example.pojo.Bill;
import com.example.pojo.BillItem;
import com.example.pojo.BillResponse;
import com.example.pojo.Customer;
import com.example.service.DiscountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
public class DiscountController {
    @Autowired
    private DiscountService discountService;

    @GetMapping("/getCustomer")
    public Customer getCustomer(@RequestParam String idCard) {
        Customer customer = discountService.getCustomer(idCard);
        if (customer == null) {
            return new Customer();
        }
        return customer;
    }

    @GetMapping(value = "/getVIP",
            consumes = MediaType.APPLICATION_XML_VALUE,
            produces = MediaType.APPLICATION_XML_VALUE)
    public Customer getVIP(@RequestParam String idCard) {
        //获取customer信息
        Customer customer = discountService.getCustomer(idCard);
        if (customer.getVipType() == null) {
            return null;
        } else {
            return customer;
        }
    }

    @GetMapping("/discount")
    public String getDiscount(@RequestParam String itemName,
                              @RequestParam BigDecimal price,
                              @RequestParam String vipLevel) {
        try {
            BigDecimal currentPrice = discountService.discount(vipLevel, itemName, price);
            return currentPrice.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }

    @GetMapping("/bills")
    public List<Bill> getBills(@RequestBody Long customerId) {
        return discountService.findBillByCustomerId(customerId);
    }

    @PostMapping("/bill")
    public Bill post(@RequestParam Bill bill) throws Exception {
        Long customerId = bill.getCustomerId();
        Customer customer = discountService.getCustomer(customerId);
        String vipType = customer.getVipType();
        BigDecimal totalPrice = BigDecimal.valueOf(0);
        for (BillItem billItem : bill.getBillItemList()
        ) {
            BigDecimal discountPrice = discountService.discount(vipType, billItem.getItemName(), billItem.getPrice());
            billItem.setDiscountedPrice(discountPrice);
            totalPrice = totalPrice.add(discountPrice);
        }
        bill.setTotal_price(totalPrice);
        return bill;
    }

    @PostMapping("/calculate_price")
    public Bill calculatePrice(@RequestBody Bill bill) {
        Long customerId = bill.getCustomerId();
        Customer customer = discountService.getCustomer(customerId);
        String vipType = customer.getVipType();
        BigDecimal totalPrice = BigDecimal.valueOf(0);
        for (BillItem billItem : bill.getBillItemList()) {
            BigDecimal discountPrice = discountService.discount(vipType, billItem.getItemName(), billItem.getPrice());
            billItem.setDiscountedPrice(discountPrice);
            totalPrice = totalPrice.add(discountPrice);
        }
        bill.setTotal_price(totalPrice);


        BillResponse billResponse=
        return bill;
    }
}
